Correct over-squaring in internal coordinate objective function for adding an atom to XYZ#802
Merged
Correct over-squaring in internal coordinate objective function for adding an atom to XYZ#802
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR fixes an over-squaring issue in the internal coordinate objective function used when adding atoms to XYZ structures. The angular constraint functions were returning squared errors that were then squared again in the objective function, leading to fourth-power minimization instead of the intended squared error minimization.
Key changes:
- Refactored
angle_constraintto return linear difference instead of squared error - Modified
dihedral_constraintto return magnitude of error vector instead of squared sum - Added precision constants to improve code maintainability
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
ee86960 to
708e750
Compare
The objective function for adding a new atom previously led to over-penalization of angular errors (effectively minimizing the error to the fourth power). This was due to the angular constraint functions (g_theta and g_phi) already returning squared errors, which were then squared again in the final objective function. This commit refactors the constraint functions to return the linear error or linear error magnitude: - `angle_constraint`: Now returns the linear difference (calc_angle - target_angle). - `dihedral_constraint`: Now returns the magnitude of the sine/cosine error vector (square root of the squared sum). The main objective function remains structurally the same (sum of squared, scaled terms), but now correctly implements a standard **relative squared error** minimization.
LeenFahoum
approved these changes
Dec 16, 2025
Contributor
LeenFahoum
left a comment
There was a problem hiding this comment.
Code looks good. Do we want to keep the comments in the code? (e.g. "wrapped to the range..)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The objective function for adding a new atom previously led to over-penalization of angular errors (effectively minimizing the error to the fourth power). This was due to the angular constraint functions (g_theta and g_phi) already returning squared errors, which were then squared again in the final objective function.
This commit refactors the constraint functions to return the linear error or linear error magnitude:
angle_constraint: Now returns the linear difference (calc_angle - target_angle).dihedral_constraint: Now returns the magnitude of the sine/cosine error vector (square root of the squared sum).The main objective function remains structurally the same (sum of squared, scaled terms), but now correctly implements a standard relative squared error minimization.